package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.admin.api.feign.RemoteDeptService;
import com.engine4cloud.tdf.common.core.constant.SecurityConstants;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.core.util.RetOps;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.OrgGroupSidManageConvert;
import com.engine4cloud.tdf.supervision.dto.create.OrgGroupSidManageCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.OrgGroupSidManageQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.OrgGroupSidManageResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.OrgGroupSidManageUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.OrgGroupSidManageEntity;
import com.engine4cloud.tdf.supervision.mapper.OrgGroupSidManageMapper;
import com.engine4cloud.tdf.supervision.service.OrgGroupSidManageService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 机构集团归属配置管理表
 *
 * @author wenjie
 * @date 2024-05-10 17:56:15
 */
@Service
public class OrgGroupSidManageServiceImpl extends ServiceImpl<OrgGroupSidManageMapper, OrgGroupSidManageEntity> implements OrgGroupSidManageService {
    @Resource
    private RemoteDeptService remoteDeptService;

    @Override
    public PageInfo<OrgGroupSidManageResultDTO> pageList(Params pageable, OrgGroupSidManageQueryDTO dto) {
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(dto.getDeptId(), SecurityConstants.FROM_IN);

        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        Page<OrgGroupSidManageEntity> page = page(Pages.page(pageable), wrapper(dto, deptIdList));
        return Pages.convert(page, OrgGroupSidManageConvert.INSTANCE::convert);
    }

    @Override
    public OrgGroupSidManageResultDTO getById(Long id) {
        return OrgGroupSidManageConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(OrgGroupSidManageCreateDTO dto) {
        TdfUser user = SecurityUtils.getUser();
        return super.save(OrgGroupSidManageConvert.INSTANCE.create(dto, user.getTenantId()));
    }

    @Override
    public Boolean update(OrgGroupSidManageUpdateDTO dto) {
        Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return super.updateById(OrgGroupSidManageConvert.INSTANCE.update(dto));
    }

    private LambdaQueryWrapper<OrgGroupSidManageEntity> wrapper(OrgGroupSidManageQueryDTO query, List<Long> deptIdList) {
        LambdaQueryWrapper<OrgGroupSidManageEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(OrgGroupSidManageEntity::getId, v));
        Queries.accept(deptIdList, v -> wrapper.in(OrgGroupSidManageEntity::getDeptId, v));
        Queries.accept(query.getGroupCode(), v -> wrapper.eq(OrgGroupSidManageEntity::getGroupCode, v));
        Queries.accept(query.getDeptName(), v -> wrapper.like(OrgGroupSidManageEntity::getDeptName, v));
        return wrapper;
    }
}